define(["require", "exports", "vscode-languageclient/client", "vscode-languageserver-protocol"], function (require, exports, client_1, vscode_languageserver_protocol_1) {
    /* --------------------------------------------------------------------------------------------
     * Copyright (c) Microsoft Corporation. All rights reserved.
     * Licensed under the MIT License. See License.txt in the project root for license information.
     * ------------------------------------------------------------------------------------------ */
    'use strict';
    Object.defineProperty(exports, "__esModule", { value: true });
    class ConfigurationFeature {
        constructor(_client) {
            this._client = _client;
        }
        fillClientCapabilities(capabilities) {
            capabilities.workspace = capabilities.workspace || {};
            capabilities.workspace.configuration = true;
        }
        initialize() {
            let client = this._client;
            client.onRequest(vscode_languageserver_protocol_1.ConfigurationRequest.type, (params, token) => {
                let configuration = (params) => {
                    let result = [];
                    for (let item of params.items) {
                        let resource = item.scopeUri !== void 0 && item.scopeUri !== null ? this._client.protocol2CodeConverter.asUri(item.scopeUri) : undefined;
                        result.push(this.getConfiguration(resource, item.section !== null ? item.section : undefined));
                    }
                    return result;
                };
                let middleware = client.clientOptions.middleware.Workspace;
                return middleware && middleware.configuration
                    ? middleware.configuration(params, token, configuration)
                    : configuration(params, token);
            });
        }
        getConfiguration(resource, section) {
            let result = null;
            if (section) {
                let index = section.lastIndexOf('.');
                if (index === -1) {
                    result = client_1.vscode.workspace.getConfiguration(undefined, resource).get(section);
                }
                else {
                    let config = client_1.vscode.workspace.getConfiguration(section.substr(0, index));
                    if (config) {
                        result = config.get(section.substr(index + 1));
                    }
                }
            }
            else {
                let config = client_1.vscode.workspace.getConfiguration(undefined, resource);
                result = {};
                for (let key of Object.keys(config)) {
                    if (config.has(key)) {
                        result[key] = config.get(key);
                    }
                }
            }
            if (!result) {
                return null;
            }
            return result;
        }
    }
    exports.ConfigurationFeature = ConfigurationFeature;
});
